package com.api.syh56.shengyunhui.handler;

import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.ResultCode;
import com.api.syh56.shengyunhui.exception.BaseException;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * RestController全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(JWTDecodeException.class)
    public CommonResult testException(JWTDecodeException jwtDecodeException) {
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    @ExceptionHandler(SignatureVerificationException.class)
    public CommonResult testException(SignatureVerificationException signatureVerificationException) {
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    @ExceptionHandler(TokenExpiredException.class)
    public CommonResult testException(TokenExpiredException tokenExpiredException) {
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    @ExceptionHandler(AlgorithmMismatchException.class)
    public CommonResult testException(AlgorithmMismatchException AlgorithmMismatchException) {
        return CommonResult.failed(ResultCode.UNAUTHORIZED);
    }

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public CommonResult<String> exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return CommonResult.failed(ex.getMessage());
    }

    /**
     * 捕获运行异常
     * @param ex
     * @return
     */
//    @ExceptionHandler
//    public CommonResult<String> exceptionHandler(Exception ex){
//        log.error("异常信息：{}", ex.getMessage());
//        return CommonResult.failed(ex.getMessage());
//    }


    /**
     * 捕获HttpMessage不可读异常
     * @param hx
     * @return
     */
    @ExceptionHandler
    public CommonResult<String> exceptionHandler(HttpMessageNotReadableException hx){
        log.error("异常信息：{}", hx.getMessage());
        return CommonResult.failed(hx.getMessage());
    }


    /**
     * 处理SQL异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public CommonResult<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        //Duplicate entry 'zhangsan' for key 'employee.idx_username'
        String message = ex.getMessage();
        if(message.contains("Duplicate entry")){
            String[] split = message.split(" ");
            String username = split[2];
            String msg = username + "已存在";
            return CommonResult.failed(msg);
        }else{
            return CommonResult.failed("未知错误");
        }
    }
}
